package com.iwords.android;

import java.net.URL;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

/**
 * 句库提供XML格式数据接口。
 */
public class ParserJukuu {
	/** 修改HTML内容。 */
	public static String query(String word) {
		// System.out.println("ParserJukuu.query()!");
		WordIllustration wi = new WordIllustration();
		String qurl = "http://xml.jukuu.com/xml_taobao.php?q=" + word;
		try {
			//long time = System.currentTimeMillis();
			//System.out.println(time);
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser parser = factory.newSAXParser();
			XMLReader xmlreader = parser.getXMLReader();
			URL url = new URL(qurl);
			InputSource is = new InputSource(url.openStream());
			is.setEncoding("utf-8");
			xmlreader.setContentHandler(new JukuuHandler(wi));
			xmlreader.parse(is);
			//System.out.println("Time consumed : "+(System.currentTimeMillis()-time));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return html(wi);
	}
	private static String html(WordIllustration wi){
		StringBuilder sb = new StringBuilder();
		sb.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">\n");
		sb.append("<head>\n");
		sb.append("<meta http-equiv=\"Content-Type\" content=\"application/vnd.wap.xhtml+xml;charset=UTF-8\"/>");
		sb.append("<meta http-equiv=\"Cache-Control\" content=\"no-cache\"/>");
		sb.append("</head>\n");
		sb.append("<body>\n");
		sb.append("<div align=\"left\">句库例句</div>");
		sb.append("<hr>\n");
		sb.append("<ol>\n");
		for(WordIllustration.Sentence sentence : wi.getSentences()){
			sb.append("<li>");
			sb.append(sentence.en+"<BR>\n");
			sb.append(sentence.cn+"<BR>\n");
			sb.append("<hr>\n");
		}
		sb.append("</ol>\n");
		sb.append("<hr>\n");
		sb.append("<div align=\"left\"><a href=\"http://jukuu.com\">查看更多酷句</a></div>\n");
		sb.append("</body>\n");
		sb.append("</html>\n");
		return sb.toString();
	}
	static class JukuuHandler extends DefaultHandler {

		boolean q = false;
		boolean chr = false;
		boolean eng = false;
		WordIllustration wi;
		String engStr;
		String chrStr;
		String spelling;
		public JukuuHandler(WordIllustration wi){
			this.wi = wi;
		}
		
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {

			//System.out.println("Start Element :" + qName);
			if (qName.equalsIgnoreCase("q")) {
				q = true;
			}else if (qName.equalsIgnoreCase("chr")) {
				chr = true;
			}else if (qName.equalsIgnoreCase("eng")) {
				eng = true;
			}
		}

		public void endElement(String uri, String localName, String qName)
				throws SAXException {
			if("item".equals(qName)){
				wi.getSentences().add(new WordIllustration.Sentence(engStr, chrStr));
			}
			//System.out.println("End Element :" + qName);
		}

		public void characters(char ch[], int start, int length)
				throws SAXException {

			if (q) {
				spelling = new String(ch, start, length);
				q = false;
			}

			if (chr) {
				chrStr = new String(ch, start, length)
				.replace("{highlight}", "<B>")
				.replace("{/highlight}", "</B>");
				chr = false;
			}

			if (eng) {
				engStr = new String(ch, start, length)
				.replace("{highlight}", "<B>")
				.replace("{/highlight}", "</B>");
				eng = false;
			}
		}

	}
}

/**句库的数据接口格式
<document>
<count>10</count>
<searchTime>0</searchTime>
<q>take care</q>

<result>
<item>
<chr>
<![CDATA[有这么一条金科玉律:只要你能照顾好自己的客户以及那些照顾客户的员工,那么,公司的成长和利润就会自然而然照顾到你。]]>
</chr>
<eng>
<![CDATA[Operate on the credo: {highlight}Take{/highlight} {highlight}care{/highlight} of your 

customers and the people who {highlight}take{/highlight} {highlight}care{/highlight} of your 

customers and the growth and profit will {highlight}take{/highlight} {highlight}care{/highlight} of 

themselves.]]>
</eng>
<src><![CDATA[www.jukuu.com]]></src>
</item>

<item>
<chr><![CDATA[小心,小心,小心。
]]></chr>
<eng><![CDATA[{highlight}Take{/highlight} {highlight}care{/highlight}, {highlight}take

{/highlight} {highlight}care{/highlight}, {highlight}take{/highlight} {highlight}care{/highlight}.]]>
</eng>
<src><![CDATA[www.jukuu.com]]></src>
</item>

<item>
<chr>
<![CDATA[有这么一条金科玉律:只要你能照顾好自己的客户以及那些照顾客户的员工,那么,公司的成长和利润就

会自然而然照顾到你。
]]>
</chr>
<eng>
<![CDATA[Operate on the credo: {highlight}Take{/highlight} {highlight}care{/highlight} of your 

customers and the people who {highlight}take{/highlight} {highlight}care{/highlight} of your 

customers and the growth and profit will {highlight}take{/highlight} {highlight}care{/highlight} of 

themselves]]>
</eng>
<src><![CDATA[www.jukuu.com]]></src>
</item>

<item>
<chr>
<![CDATA[我宁愿照顾活人的肚子,也不愿以石碑的形式来照顾死者的荣耀。
]]>
</chr>
<eng>
<![CDATA[I'd rather {highlight}take{/highlight} {highlight}care{/highlight} of the stomachs of the 

living than ({highlight}take{/highlight} {highlight}care{/highlight} of) the glory of the dead in the 

form of stone memorials.]]>
</eng>
<src><![CDATA[www.jukuu.com]]></src>
</item>

<item>
<chr>
<![CDATA[小事留意,大事顺利。
]]>
</chr>
<eng>
<![CDATA[{highlight}Take{/highlight} {highlight}care{/highlight} of the pence, and the pounds ill 

{highlight}take{/highlight} {highlight}care{/highlight} of themselves.]]>
</eng>
<src><![CDATA[www.jukuu.com]]></src>
</item>

</result>
</document>
 */